//
//  AlarmClockTableViewCell.m
//  AlarmClock
//
//  Created by apple on 16/9/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AlarmClockTableViewCell.h"
#import "BSKConsts.h"
#import "SDAutoLayout.h"
#import "UILocalNotification+setAlarmclock.h"
@implementation AlarmClockTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initializeAperance];
    }
    return self;
}

#pragma mark - private method
- (void)initializeAperance {
    [self.contentView addSubview:self.alarmClockLabel];
    [self.contentView addSubview:self.numberLabel];
    [self.contentView addSubview:self.startLabel];
    [self.contentView addSubview:self.alarmSwitch];
    
    self.alarmClockLabel.sd_layout
    .leftSpaceToView(self.contentView,20)
    .topSpaceToView(self.contentView,20)
    .widthIs(100)
    .heightIs(30);
    
    self.numberLabel.sd_layout
    .leftSpaceToView(self.alarmClockLabel,0)
    .topSpaceToView(self.contentView,0)
    .widthIs(100)
    .heightIs(80);
    
    self.startLabel.sd_layout
    .leftSpaceToView(self.contentView,20)
    .topSpaceToView(self.alarmClockLabel,0)
    .widthIs(100)
    .heightIs(40);
    
    self.alarmSwitch.sd_layout
    .rightSpaceToView(self.contentView,0)
    .topSpaceToView(self.contentView,35)
    .widthIs(100)
    .heightIs(30);
    self.startLabel.sd_layout.centerXIs(self.contentView.centerX-self.startLabel.width);
}

#pragma mark - action
- (void)controlProgress:(UISwitch *)startSwitch {
    NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"alarmClock"] mutableCopy];
    NSMutableArray *newArray = @[].mutableCopy;
    if (_alarmSwitch.on) {
        self.alarmClockLabel.textColor = [UIColor whiteColor];
        self.startLabel.text = @"开启";
        
        for (NSDictionary *dic in array) {
            NSMutableDictionary *dic1 = [dic mutableCopy];
            if ([self.dateId isEqualToString:dic[@"alarmClockId"]]) {
                [dic1 setObject:@"YES" forKey:@"isStart"];
            }
            [newArray addObject:dic1];
        }
        // 发送本地通知
        [UILocalNotification createAlrmClockWhithInfoDic:newArray[self.index]];
    } else {
        self.alarmClockLabel.textColor = [UIColor grayColor];
        self.startLabel.text = @"未开启";
        for (NSDictionary *dic in array) {
            NSMutableDictionary *dic1 = [dic mutableCopy];
            if ([self.alarmClockLabel.text isEqualToString:dic[@"date"]]) {
                [dic1 setObject:@"NO" forKey:@"isStart"];
            }
            [newArray addObject:dic1];
        }
        
        // 关闭闹钟
        NSArray * notis = [UIApplication sharedApplication].scheduledLocalNotifications;
        for (UILocalNotification * noti in notis) {
            if ([[noti.userInfo objectForKey:@"alarmClockId"] isEqualToString:self.dateId]) {
                [[UIApplication sharedApplication] cancelLocalNotification:noti];
            }
        }
    }
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"alarmClock"];
    [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"alarmClock"];
}
#pragma mark - getter
- (UILabel *)alarmClockLabel {
    if (_alarmClockLabel) {
        return _alarmClockLabel;
    }
    _alarmClockLabel = [[UILabel alloc]init];
    _alarmClockLabel.textColor = [UIColor whiteColor];
    //_alarmClockLabel.text = @"07:30";
    _alarmClockLabel.font = [UIFont systemFontOfSize:30 weight:0.1];
    return _alarmClockLabel;
}

- (UILabel *)numberLabel {
    if (_numberLabel) {
        return _numberLabel;
    }
    _numberLabel = [[UILabel alloc]init];
    _numberLabel.textColor = [UIColor whiteColor];
    _numberLabel.font = [UIFont systemFontOfSize:15];
    _numberLabel.numberOfLines = 0;
    [_numberLabel sizeToFit];
    //_numberLabel.text = @"只响一次";
    _numberLabel.textColor = [UIColor colorWithRed:0.5115 green:0.5197 blue:0.5303 alpha:1.0];
    return _numberLabel;
}

- (UILabel *)startLabel {
    if (_startLabel) {
        return _startLabel;
    }
    _startLabel = [[UILabel alloc]init];
    _startLabel.textColor = [UIColor whiteColor];
    _startLabel.font = [UIFont systemFontOfSize:15];
    _startLabel.text = @"开启";
    _startLabel.textColor = [UIColor colorWithRed:0.5115 green:0.5197 blue:0.5303 alpha:1.0];
    return _startLabel;
}

- (UISwitch *)alarmSwitch {
    if (_alarmSwitch) {
        return _alarmSwitch;
    }
    _alarmSwitch = [[UISwitch alloc]init];
    //_alarmSwitch.center = CGPointMake(ScreenWidth-50, CGRectGetMidY(self.frame));
    //_alarmSwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
    _alarmSwitch.onTintColor = [UIColor colorWithRed:0.2032 green:0.4936 blue:0.4936 alpha:1.0];
    _alarmSwitch.thumbTintColor = [UIColor whiteColor];
    //_alarmSwitch.on = YES;
    [_alarmSwitch addTarget:self action:@selector(controlProgress:) forControlEvents:UIControlEventValueChanged];
    return _alarmSwitch;
}

@end
